
package data;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;
import javax.servlet.http.HttpServletRequest;
 import org.apache.struts.action.ActionForm;
 import org.apache.struts.action.ActionMapping;
/**
 *
 * @author Alejandro
 */
public class ModificarBitacora1Form extends ActionForm{
    private String cedula;

    public void setCedula(String cedula) {
        this.cedula = cedula;
    }

    public String getCedula() {
        return cedula;
    }
    
    @Override
    public void reset(ActionMapping mapping, HttpServletRequest request){
        this.cedula = null;
    }
    
    @Override
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if (cedula.length() != 11) {
            errors.add("cedula", new ActionMessage("error.cedula.fail"));
        } 
        if (cedula.length() == 11){
            boolean correcto = true;
            for(int i=0;i<11;i++){
                if(i==1 || i==6){
                    if(cedula.charAt(i)!='-') correcto = false;
                } else {
                    if(!Character.isDigit(cedula.charAt(i))) correcto = false;
                }
            }
            if(!correcto) errors.add("cedula", new ActionMessage("error.cedula.fail"));
            
            BitacoraDataMapper mapper = new BitacoraDataMapper();
            if (!mapper.verificarCedulaProf(cedula)) {
                errors.add("cedula", new ActionMessage("error.cedula.nonexistent"));
            }
        }
        return errors;
    }
}
